<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页面</title>
    <!-- 引入axios -->
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f2f5;
        }
        * 容器样式 */
        .container {
            background-color: #f9f9f9;
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            width: 350px;
            text-align: center;
            transition: transform 0.3s ease;
        }

        .container:hover {
            transform: translateY(-5px);
        }

        /* 标题样式 */
        .container h2 {
            margin-bottom: 20px;
            font-size: 24px;
            color: #444;
        }

        /* 输入框样式 */
        .container input {
            width: 100%;
            padding: 12px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.3s ease, box-shadow 0.3s ease;
        }

        .container input:focus {
            border-color: #6a11cb;
            box-shadow: 0 0 8px rgba(106, 17, 203, 0.3);
            outline: none;
        }

        #login-form, #register-form {
            background: white;
            padding: 2rem;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        .form-group {
            margin-bottom: 1rem;
        }

        label {
            display: block;
            margin-bottom: 0.5rem;
            color: #333;
        }

        input {
            width: 100%;
            padding: 0.5rem;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        button {
            width: 100%;
            padding: 0.75rem;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }

        .btn:hover {
            background-color: #0056b3;
        }
        /* 切换按钮样式 */
        .toggle-button {
            background-color: transparent;
            border: none;
            color: #6a11cb;
            cursor: pointer;
            font-size: 14px;
            text-decoration: underline;
            transition: color 0.3s ease;
        }

    
        .error-message {
            color: #dc3545;
            margin-top: 1rem;
            text-align: center;
        }
        /* 隐藏表单 */
        .hidden {
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- 登录表单 -->
        <div id="login-form" >
            <h2 >用户登录</h2>
            <form id="loginForm" onsubmit="handleSubmit(event)">
                <div class="form-group">
                    <label for="username">用户名：</label>
                    <input type="text" id="login-username" required>
                </div>
                <div class="form-group">
                    <label for="password">密码：</label>
                    <input type="password" id="login-password" required>
                </div>
                <button class="btn" type="submit">登录</button>
            </form>
            <button class="toggle-button" onclick="toggleForm('register-form')">注册</button>
            <div id="errorMessage" class="error-message"></div>
        </div>

        <!-- 注册表单 -->
        <div id="register-form" class="hidden">
            <h2>用户注册</h2>
            <form id="registerForm" onsubmit="">
                <div class="form-group">
                    <label for="username">用户名：</label>
                    <input type="text" id="register-username" required>
                </div>
                <div class="form-group">
                    <label for="password">密码：</label>
                    <input type="password" id="register-password" required>
                    <input type="password" id="register-confirm-password" placeholder="确认密码">
                </div>
                
                <button class="btn" type="submit">注册</button>
            </form>
            <button class="toggle-button" onclick="toggleForm('login-form')">登录</button>
            <div id="errorMessage" class="error-message"></div>
        </div>

    </div>
    
    <script>
        // 切换表单显示
        function toggleForm(formId) {
            document.getElementById('login-form').classList.toggle('hidden');
            document.getElementById('register-form').classList.toggle('hidden');
        }

        function handleSubmit(event) {
            event.preventDefault(); // 阻止表单默认提交行为
            
            const username = document.getElementById('login-username').value;
            const password = document.getElementById('login-password').value;
            const errorMessage = document.getElementById('errorMessage');
            
            // 清除之前的错误信息
            errorMessage.textContent = '';
    
            axios({
                method: 'post',
                url: 'http://127.0.0.1:5000/login',
                headers: {
                    'Content-Type': 'application/json',
                },
                data: {
                    username: username,
                    password: password
                }
            })
            .then(function (response) {
                // 处理成功响应
                if (response.data.code === 200) { // 假设返回数据中包含code字段
                    window.location.href = '/home'; // 登录成功跳转到首页
                } else {
                    errorMessage.textContent = response.data.error || '登录失败';
                }
            })
            .catch(function (error) {
                console.log(error);
                // 处理错误
                if (error.response) {
                    // 服务器返回了非常态响应
                    errorMessage.textContent = error.response.data.error || '系统错误';
                } else {
                    errorMessage.textContent = '网络错误，请检查连接';
                }
            });
        }
    </script>

    
</body>
</html>